package com.example.demo.utils;

import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;

/**
 * @author linjingze
 * @date 2023/12/21 11:33 AM
 */
public class TextFieldUtils {

    public static TextField createDecimalTextField() {
        TextField decimalTextField = new TextField();
        decimalTextField.addEventFilter(KeyEvent.KEY_TYPED, keyEvent -> {
            if (!keyEvent.getCharacter().matches("[0-9.]")) {
                keyEvent.consume();
            }
            // 防止输入多个小数点
            if (keyEvent.getCharacter().equals(".") && decimalTextField.getText().contains(".")) {
                keyEvent.consume();
            }
        });
        return decimalTextField;
    }
    public static TextField createHiddenField() {
        TextField hiddenField = new TextField();
        hiddenField.setVisible(false); // 设置为不可见
        hiddenField.setManaged(false); // 确保
        return hiddenField;
    }
}
